package com.ssm.wechat.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.bean.ResponseTextMessage;
import com.ssm.pre.bean.Template;
import com.ssm.pre.bean.TemplateParam;
import com.ssm.pre.dao.WechatKeysDao;
import com.ssm.pre.service.FamilyService;
import com.ssm.pre.util.MessageSendUtil;
import com.ssm.pre.util.MessageUtil;
import com.ssm.util.Constants;
import com.ssm.wechat.service.WechatLifeService;
import com.ssm.wechat.service.WechatService;
import com.ssm.wechat.service.WechatUserService;

@Service
public class WechatServiceImpl implements WechatService {
	
	@Autowired
	private WechatKeysDao wechatKeysDao;
	
	@Autowired
	private WechatUserService wechatUserService;
	
	@Autowired
	private WechatLifeService wechatLifeService;
	/**
	 * 对微信平台转发过来的微信用户发送的消息进行处理
	 */
	@Override
	public String WechatReply(HttpServletRequest request) throws Exception {
		String respMessage = "你好！";
		
		try{
			String respContent = "欢迎使用鼎兴物业鼎鼎机器人";
			Map<String, String> requestMap = MessageUtil.parseXml(request);// xml请求解析
			String fromUserName = requestMap.get("FromUserName");// 发送方帐号（open_id）
			String toUserName = requestMap.get("ToUserName");// 公众帐号
			String msgType = requestMap.get("MsgType");// 消息类型

			// 回复文本消息
			ResponseTextMessage textMessage = new ResponseTextMessage();
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(Constants.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setFuncFlag(0);
			
			if(!msgType.equals(Constants.REQ_MESSAGE_TYPE_TEXT)&&!msgType.equals(Constants.REQ_MESSAGE_TYPE_EVENT)){
				return MessageSendUtil.Reback(fromUserName, toUserName, "", request);
			}
			if (msgType.equals(Constants.REQ_MESSAGE_TYPE_TEXT)) {//文本消息
				String content = requestMap.get("Content");
				Map<String, Object> respKey = new HashMap<>();
				respKey.put("wechatKey", content);
				//关键字查询：根据请求获取到回应的信息
				respKey = wechatKeysDao.findKeysByReq(respKey);
		       if(respKey == null){//如果关键字信息为空，返回用户请求信息
		    	   return MessageSendUtil.Reback(fromUserName, toUserName, content, request);
		       }else{//如果关键字不为空，按照请求信息返回信息
		    	   respContent = respKey.get("context").toString();
		       }
			} else if (msgType.equals(Constants.REQ_MESSAGE_TYPE_IMAGE)) {// 图片消息
				respContent = "您好，该公众号目前作为开发使用";
			} else if (msgType.equals(Constants.REQ_MESSAGE_TYPE_LOCATION)) {//地理位置消息
				respContent = "您好，该公众号目前作为开发使用";
			} else if (msgType.equals(Constants.REQ_MESSAGE_TYPE_LINK)) {// 链接消息
				respContent = "您好，该公众号目前作为开发使用";
			} else if (msgType.equals(Constants.REQ_MESSAGE_TYPE_VOICE)) {// 音频消息
				respContent = "您好，该公众号目前作为开发使用";
			}
			if (msgType.equals(Constants.REQ_MESSAGE_TYPE_EVENT)) {// 事件推送
				String eventType = requestMap.get("Event");// 事件类型
				if (eventType.equals(Constants.EVENT_TYPE_LOCATION)){//获取地理位置
					respContent = "您好，该公众号目前作为开发使用";
				}else if (eventType.equals(Constants.EVENT_TYPE_SUBSCRIBE)) {// 订阅
					wechatUserService.subscribe(fromUserName);
				} else if (eventType.equals(Constants.EVENT_TYPE_UNSUBSCRIBE)) {// 取消订阅------取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息
					wechatUserService.unsubscribe(fromUserName);
				} else if (eventType.equals(Constants.EVENT_TYPE_CLICK)) {// 自定义菜单点击事件  回复一个模板消息
					//事件KEY值，与创建自定义菜单时指定的KEY值对应
                    String eventKey = requestMap.get("EventKey");
                    if(eventKey.equals("20171116092814")){//查询物业费账单(历史记录)
                    	//查询物业费账单
//                    	boolean result = wechatLifeService.sendPropertyMsg(tem);
                    	
                    }else if(eventKey.equals("20171116092759")){//电费查询
//                    	respContent = "电费查询";
                    }
				}
//				return respContent;
			}
			textMessage.setContent(respContent);
			respMessage = MessageUtil.textMessageToXml(textMessage);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return respMessage;
	}
	

}
